home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Over 1,000 Windows 95 Programs
/
Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso
/
1244
/
procapp3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-17
|
8KB
|
261 lines
#include <windows.h>
#include <malloc.h>
#include "procbox.h"
////////////////////////////////////////////////////////////////////////////////
//
// PROCAPP3.C - Demonstrates use of Process Box Low-Level Interface
//
// - Contains code for
// - WinMain Entry Point
// - WndProc
HINSTANCE hinst;
typedef struct _tagUserData
{
FARPROC lpfnPBCallback;
WORD process_count;
} USERDATA; // data kept by each process box
int iProcBoxCount = 0; // incremented for each new process box
BOOL bProcessLoop = FALSE; // any active process boxes?
// If there are, we enter a special loop in
// WinMain....
LRESULT FAR PASCAL _export WndProc(HWND, UINT, WPARAM, LPARAM);
int CALLBACK _export PBCallback(HWND, int , LPARAM );
char szAppName[] = "PROCAPP";
char szWindowText[] = "ProcessBox - Low-Level Interface Demo";
/////////////////////////////////////////////////
//
//
// WndProc
//
LRESULT FAR PASCAL _export WndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HWND hwndPB;
USERDATA *pUserData;
char szText[64];
switch (message)
{
case WM_KEYDOWN:
case WM_LBUTTONDOWN:
////////////////////////
//
// CREATE PROCESSBOX:
//
wsprintf(szText, "Process #%d in progress", iProcBoxCount);
hwndPB = CreateProcessBox( hwnd, // owner
szText, // caption
NULL, // message
10+iProcBoxCount*4, // x screen coord
50+iProcBoxCount*4, // y screen coord
NULL );
if (hwndPB) // If Process Box was created OK
{
// pUserData = a chunk of Data private to each process box
pUserData = (USERDATA*)malloc (sizeof(USERDATA));
// pUserData is freed in PBCallback
if (pUserData)
{
pUserData->process_count = 0;
pUserData->lpfnPBCallback = MakeProcInstance((FARPROC)PBCallback, hinst);
// the proc-instance is freed in PBCallback also
if (pUserData->lpfnPBCallback)
AttachProcess(hwndPB, pUserData->lpfnPBCallback, (LPARAM)(void far *)pUserData);
else
{
MessageBox(hwnd, "Unable to Attach Process Callback", NULL, NULL);
DestroyProcessBox(hwndPB);
}
}
iProcBoxCount++;
bProcessLoop = TRUE; // enable the process loop
}
else
MessageBox(hwnd, "Unable to create Process Box", NULL, NULL);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam );
}
///////////////////////////////////////////////////////////
//
// Process Box Callback
//
int CALLBACK _export PBCallback(HWND hwndPB, int iCode, LPARAM lParam)
{
USERDATA far *lpUserData = (USERDATA far *)lParam;
long j;
switch (iCode)
{
case PBC_OPEN:
lpUserData->process_count = 0;
return TRUE;
case PBC_CLOSE:
if (lpUserData) // double check, just in case!
{
FreeProcInstance(lpUserData->lpfnPBCallback);
_ffree(lpUserData); // free up the user data (allocated during Process Box creation)
}
return TRUE;
case PBC_CANCEL:
return ( MessageBox(NULL, "Really cancel operation?", "Message Box",
MB_APPLMODAL|MB_YESNO)==IDYES ? TRUE : FALSE );
case PBC_PROCESS:
lpUserData->process_count++;
for (j=0; j<0xEFF; j++); // a delay
if ((lpUserData->process_count)>10000)
return PBCR_END;
SendMessage(hwndPB, PM_SETGAUGE, lpUserData->process_count/100, 0l);
return PBCR_CONTINUE;
}
return TRUE;
}
/////////////////////////////////////////////////
//
//
// WinMain - Entry Point
//
//
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
MSG msg ;
WNDCLASS wndclass ;
HWND hwnd;
HWND hwndPB; // handle to a ProcessBox
int iState; // return parameter from IterateTaskProcesses
hinst = hInstance;
if (!hPrevInstance)
{
wndclass.style = NULL ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL,IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow ( szAppName, // class
szWindowText, // window text
WS_OVERLAPPEDWINDOW, // style
CW_USEDEFAULT, CW_USEDEFAULT, // x, y start
CW_USEDEFAULT, CW_USEDEFAULT, // width, height
NULL, // parent window handle
NULL, // menu handle
hInstance, // instance handle
NULL) ; // long pointer to creation data
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
// THE STANDARD MESSAGE LOOP
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
// END OF STANDARD MESSAGE LOOP
//
// PROCESSING LOOP: Add this to enable Process Boxes to act like modal dialogs
//
while (bProcessLoop) // this is set in WndProc case WM_LBUTTONDOWN:
{
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return msg.wParam;
if (!IsProcessMessage(NULL, &msg)) // add IsDialogMessage here if you have modeless dialogs
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} // end if (PeekMessage)
hwndPB = IterateTaskProcesses(&iState); // This is where processing for all the
// Process Boxes occurs
//
// * see the comments below
if (hwndPB==NULL) // no more Process Boxes
bProcessLoop = FALSE; // exit the processing loop
else if (iState!=PI_CONTINUE) // This hwndPB returned PI_END,PI_ERROR or PI_CANCEL from IterateTaskProcesses
DestroyProcessBox(hwndPB); // -> Just destroy it
}// end while (bProcessLoop)
//
// END OF PROCESSING LOOP
//
} // end while (GetMessage)
return msg.wParam ;
}
//
// !!!!!!!!!!! NOT USED !!!!!!!!!!!!!!!!!!!!!!!!
// FOR EXPLANATION ONLY:
//
// * RE: IterateTaskProcesses - this function is basically a wrapper around
// a GetProcessBox(First/Next), IterateProcess loop:
//
// The code below is functionally equivalent to a call IterateTaskProcesses
//
HWND YOUR_CUSTOM_IterateTaskProcesses(int far *lpState)
{
HWND hwndPB, hwndPBReturn;
hwndPB = GetProcessBoxFirst(); // find the first Process Box owned by this task
if (hwndPB)
hwndPBReturn = hwndPB;
while (hwndPB) // step through the list of Process Boxes owned by this task
{
*lpState = IterateProcess(hwndPB); // check for cancelation, send PBC_PROCESS code to callback
// returns value =
// PI_PROCESSERROR, PI_CANCEL,
// PI_END, or PI_CONTINUE
if (*lpState != PI_CONTINUE)
break;
hwndPB = GetProcessBoxNext(hwndPB); // get next process box owned by this task
if (hwndPB)
hwndPBReturn = hwndPB;
}
return hwndPBReturn;
}